<?php
	$cur_page_ver="0.5d";
	$this_page="profile";
	$this_title="Profiles";
	$center_main=true;
	require_once('includes/global.php');
	if(isset($_GET['action']) && ($_GET['action']=='view' || $_GET['action']=='add' || $_GET['action']=='edit'))
	{
		$action=$_GET['action'];
	}
	else
	{
		$action='view';
	}
	if(isset($_GET['resetall']) && $_GET['resetall']==true)
	{
		unset($_SESSION['reg']);
		$page=1;
	}
	switch($action)
	{
		case 'view':
			isset($_GET['user'])?$user=$_GET['user']:$user=null;
			is_numeric($user)?$result=$dbcon->query("SELECT UN,profiles.* FROM users LEFT JOIN profiles ON users.UID=profiles.UID WHERE users.uid=$user",1):$result=$dbcon->query("SELECT UN,profiles.* FROM users LEFT JOIN profiles ON users.UID=profiles.UID WHERE users.UN='$user'",1);
			$age=(int)date("Y")-(int)substr($result['dob'],0,4);
			(int)date("m")<(int)substr($result['dob'],5,2)?$age--:null;
			(date('m')==date("m",$result['dob']))&&(date("d")<date("d",$result['dob']))?$age--:null;
			$smarty->assign('maritial',$result['mstatus']);
			$smarty->assign('email',$result['email']);
			$smarty->assign('realname',$result['name']);
			$smarty->assign('location',$result['location']);
			$smarty->assign('gender',$result['gender']);
			$smarty->assign('occupation',$result['occupation']);
			$smarty->assign('hobbies',$result['hobbies']);
			$quotesArr=explode('\r',$result['quotes']);
			for($x=0;$x<count($quotesArr);$x++)
			{
				$x==0?$quotes=$quotesArr[$x]:$quotes.='<br />'.$quotesArr[$x];
			}
			$smarty->assign('quotes',$quotes);

			
			$smarty->assign('news',$result['news']);
			$smarty->assign('homepage',$result['homepage']);
			$linksArr=explode('\r',$result['links']);
			for($x=0;$x<count($linksArr);$x++)
			{
				//echo $linksArr[$x];
				if($x==0)
				{
					if(strtolower(substr($linksArr[$x],0,4))=='http' ||strtolower(substr($linksArr[$x],0,4))=='ftp')
					{
						$links='<a href="'.$linksArr[$x].'">'.$linksArr[$x].'</a>';
					} else {
						$links='<a href="http://'.$linksArr[$x].'">'.$linksArr[$x].'</a>';
					}
				} else {
					if(strtolower(substr($linksArr[$x],0,4))=='http' ||strtolower(substr($linksArr[$x],0,4))=='ftp')
					{
						$links.='<br /><a href="'.$linksArr[$x].'">'.$linksArr[$x].'</a>';
					} else {
						$links.='<br /><a href="http://'.$linksArr[$x].'">'.$linksArr[$x].'</a>';
					}
				}
			}
			//echo $links;
			$smarty->assign('links',$links);
			$smarty->assign('age',$age);
			$smarty->assign('username',$result['UN']);
			break;
			
		case 'add':
			$jscript="function ResetAll()
{
	window.location = \"profile.php?action=add&resetall=true\";
}\r";
			$page=isset($_GET['page'])?$_GET['page']:1;
			//Now we get to the real coding
			switch($page)
			{
			case 1:
				//$jscript=null;
				break;
			case 2:
				if(isset($_POST['username'])&&isset($_POST['email'])&&isset($_POST['password'])&&isset($_POST['confirm']))
				{
					$username=$_POST['username'];
					$email=$_POST['email'];
					$password=$_POST['password'];
					$confirm=$_POST['confirm'];
					//Need to check username characters
					
					//Need to check for valid email address form
					if($username==null)
					{
						$page=1;
						$smarty->assign('email',$email);
						$smarty->assign('password',$password);
						$smarty->assign('confirm',$confirm);
						$smarty->assign('no_user',true);
						break;
					}
						
					$result=$dbcon->query("SELECT * from users where UN='{$username}'");
					if($result!=0)
					{
						$page=1;
						$smarty->assign('email',$email);
						$smarty->assign('password',$password);
						$smarty->assign('confirm',$confirm);
						$smarty->assign('bad_user',true);
						break;
					}
					if(!ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$",$email))
					{
						//echo "Bad eregi";
						$page=1;
						$smarty->assign('username',$username);
						$smarty->assign('password',$password);
						$smarty->assign('confirm',$confirm);
						$smarty->assign('bad_email',true);
						break;
					}
					if($email==null)
					{
						$page=1;
						$smarty->assign('username',$username);
						$smarty->assign('password',$password);
						$smarty->assign('confirm',$confirm);
						$smarty->assign('no_email',true);
						break;
					}
					if($password==null || $confirm==null)
					{
						$page=1;
						$smarty->assign('username',$username);
						$smarty->assign('email',$email);
						$smarty->assign('no_password',true);
						break;
					}
					if($password!==$confirm)
					{
						$page=1;
						$smarty->assign('username',$username);
						$smarty->assign('email',$email);
						$smarty->assign('bad_password',true);
						break;
					}
				}
				else
				{
					$page=1;
					break;
				}
					$jscript.="function setAnswerBox()
{
	//document.frmReg.customQuestion.
	if(document.frmReg.mnuQuestion.selectedIndex==7)
	{
		document.frmReg.customQuestion.disabled=false;
	} else {
		document.frmReg.customQuestion.disabled=true;
	}
}";	
				$_SESSION['reg']['username']=$username;
				$_SESSION['reg']['email']=$email;
				$_SESSION['reg']['password']=sha1($password);
				$smarty->assign('javascript',$jscript);			
				break;
			case 3:
				if(isset($_POST['mnuQuestion']) && isset($_POST['answer']))
				{
					$question=($_POST['customQuestion']==null)?getQuestion($_POST['mnuQuestion']):$_POST['customQuestion'];
					$answer=$_POST['answer'];
					if($question=='Custom')
					{
						$page=2;
						$jscript.="function setAnswerBox()
{
	//document.frmReg.customQuestion.
	if(document.getElementById(mnuQuestion).selectedIndex==7)
	{
		document.getElementById(customQuestion).disabled=false;
	} else {
		document.getElementById(customQuestion).disabled=true;
	}
}";	
						$smarty->assign('no_custom',true);
						$smarty->assign('multi',$_POST['mnuQuestion']);
						$smarty->assign('answer',$_POST['answer']);
						$smarty->assign('javascript',$jscript);
						break;
					}
					if($answer==null)
					{
						$page=2;
						$jscript.="function setAnswerBox()
{
	//document.frmReg.customQuestion.
	if(document.getElementById(mnuQuestion).selectedIndex==7)
	{
		document.getElementById(customQuestion).disabled=false;
	} else {
		document.getElementById(customQuestion).disabled=true;
	}
}";	
						$smarty->assign('no_answer',true);
						$smarty->assign('multi',$_POST['mnuQuestion']);
						$_POST['mnuQuestion']==7?$smarty->assign('custom',$_POST['customQuestion']):null;
						$smarty->assign('answer',$_POST['answer']);
						break;
					}
				}
				else
				{
					$page=1;
					break;
				}
				$_SESSION['reg']['secret']['question']=$question;
				$_SESSION['reg']['secret']['answer']=$answer;
				//Store info to the database
//				print_r($_SESSION);
				$dbcon->executeSQL("INSERT INTO `users`(UN,PW,member_since) VALUES('".$_SESSION['reg']['username']."','".$_SESSION['reg']['password']."',NOW())");
				$_SESSION['reg']['uid']=$dbcon->query("SELECT `UID` WHERE `UN`='".$_SESSION['reg']['username']."'");
				$dbcon->executeSQL("INSERT INTO `password_recovery`(UID,question,answer) VALUES(".$_SESSION['reg']['uid'].",'".$_SESSION['reg']['question']."','".$_SESSION['reg']['answer']."'");
				$dbcon->executeSQL("INSERT INTO `profiles`(email) VALUES('".$_SESSION['reg']['email']."'");
				break;
			case 4:
			
				break;
			case 5:
				break;
			case 6:
				print_r($_POST);
				if(isset($_SESSION['reg']))
				{
					if(!isset($_POST['acceptTOS']) && $_POST['acceptTOS']!='on')
					{
						$page=5;
						$smarty->assign('no_tos',true);
						break;
					}
				}
				else
				{
					$page=1;
					break;
				}
				$_SESSION['tos']=1;
				//Time to write all the DB info for the registration
				
				//$dbcon->executeSQL("INSERT INTO users(`UN`,`PW`,`member_since`) VAULES('".$_SESSION['reg']['username']."','".$_SESSION['reg']['password']."',NOW())");
				//$result=$dbcon->query("SELECT UID FROM `users` WHERE UN='".$_SESSION['reg']['username']."'");
				//$_SESSION['reg']['uid']=$result['uid'];
				//$dbcon->executeSQL("INSERT INTO `profiles`(`UID`,`email`,`name`,`location`,`dob`,`mstatus`,`gender`,`occupation`,`hobbies`,`quotes`,`news`,`homepage`,`links`) VALUES(".$_SESSION['reg']['uid']
				break;
			default:
				$page=1;
				break;
			}
			if(isset($_SESSION['reg']))
			{
				
			}
			$smarty->assign('javascript',$jscript);
			$smarty->assign('next_page',$page+1);
			$smarty->assign('reg_page',$page);
			break;
		case 'edit':
			$uid=$_SESSION['UID'];
			
			break;
	}
	//echo $action;
	$smarty->assign('action',$action);
	$smarty->display('header.tpl');
	$action!='add'?$smarty->display('profile.tpl'):$smarty->display('register.tpl');
	$smarty->display('footer.tpl');
	
	function getQuestion($num)
	{
		switch((int)$num)
		{
			case 0:
				$q="What is your mother's maiden name?";
				break;
			case 1:
				$q="What city where you born in?";
				break;
			case 2:
				$q="Who was your hero when you were younger?";
				break;
			case 3:
				$q="What is your favorite food?";
				break;
			case 4:
				$q="4";
				break;
			case 5:
				$q="5";
				break;
			case 6:
				$q="6";
				break;
			case 7:
				$q="Custom";
				break;
			default:
				$q="We have a problem here";
		}
		return $q;
	}
?>